package com.vq3.digitalizationsoftwareintegrates.Servlet.BusinessCenter.Spot_Check;


import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.MessageType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.S_CType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.StatusType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_Point_ContentEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_SaveSCDailyContextEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_WebSocketMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_WebSocketSendMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_DatesEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_Point_ContentEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_SaveDailyContextDao;
import com.vq3.digitalizationsoftwareintegrates.Service.MessageCenter.MessageCenterService;
import com.vq3.digitalizationsoftwareintegrates.Service.Spot_Check.Spot_Check_FusionService;
import com.vq3.digitalizationsoftwareintegrates.Service.User.AuditService;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserLoginService;

import com.vq3.digitalizationsoftwareintegrates.Servlet.WebSocket.WebSocketServer;
import com.vq3.digitalizationsoftwareintegrates.Utils.DateUtils.GetServerDate;
import com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil.CalculationIsType;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.IllegalFormatCodePointException;
import java.util.List;

@RestController
@RequestMapping("Spot_Check_Audit")
public class Spot_Check_AuditFusionServlet {
    @Resource
    AuditService auditService;
    @Resource
    UserLoginService userLoginService;
    @Resource
    Spot_Check_FusionService spot_check_fusionService;
    @Resource
    MessageCenterService messageCenterService;
    @Resource
    MongoDB_S_C_SaveDailyContextDao mongoDB_s_c_saveDailyContextDao;

    private List<S_C_Point_ContentEntity> getS_C_Point_ContentEntity(String itemName, UserEntity userEntity) throws IOException {

        return spot_check_fusionService.downLoadImage(itemName, userEntity, S_CType.TEMP, StatusType.Submitted);
    }

    private UserEntity getUser(@RequestParam String itemName) throws IOException {
        String uName = auditService.itemNameFindUName(itemName, StatusType.Submitted);
        return userLoginService.findUser(uName);

    }

    @RequestMapping("sportAudit")
    public void sportAudit(@RequestParam String itemName,@RequestParam String status, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        WebSocketServer webSocketServer = new WebSocketServer();
        MongoDB_S_C_WebSocketSendMessageEntity mongoDB_s_c_webSocketSendMessageEntity = new MongoDB_S_C_WebSocketSendMessageEntity();
        StatusType statusType = CalculationIsType.calculationIsStatusType(status);
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        boolean b = auditService.S_C_AuditReviewNG(itemName, statusType);
        if (b) {
            resultInfo.setFlag(true);
            resultInfo.setErrorMsg("审核成功！已驳回！");
            mongoDB_s_c_webSocketSendMessageEntity.setMessage("您的"+itemName+"被驳回,请检查！");
            GetServerDate.getMessageTime(mongoDB_s_c_webSocketSendMessageEntity);
            webSocketServer.SendPrivateMessagesToUser(mongoDB_s_c_webSocketSendMessageEntity,auditService.itemNameFindUName(itemName,StatusType.ReviewNG));
        }else{
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("审核失败");
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }
    @RequestMapping("audit")
    public void audit(@RequestParam String itemName,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
        UserEntity userEntity = getUser(itemName);
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        List<S_C_Point_ContentEntity> s_c_point_contentEntities = getS_C_Point_ContentEntity(itemName, userEntity);
        if (s_c_point_contentEntities != null) {
            if (s_c_point_contentEntities.isEmpty()){
                resultInfo.setFlag(false);
            }else {
                resultInfo.setFlag(true);
                resultInfo.setData(s_c_point_contentEntities);
            }
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }



    @RequestMapping("detailedFind")
    public void detailedFind(@RequestParam String itemName,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {

        PackagedData packagedData = new PackagedDataImpl();

        MongoDB_S_C_WebSocketMessageEntity mongoDB_s_c_webSocketMessageEntities = auditService.itemNameFindRemarkContext(itemName, StatusType.ReviewNG);
        List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntityS = mongoDB_s_c_webSocketMessageEntities.getMongoDB_s_c_point_contentEntityS();
        if (mongoDB_s_c_point_contentEntityS.size() > 0) {
            for (MongoDB_S_C_Point_ContentEntity mongoDB_s_c_point_contentEntity : mongoDB_s_c_point_contentEntityS) {
                String imageName = mongoDB_s_c_point_contentEntity.getImageName();
                mongoDB_s_c_point_contentEntity.setImageName(imageName+ ".jpeg");
            }
            packagedData.writeValue(mongoDB_s_c_webSocketMessageEntities,httpServletResponse);
        }

    }

    @RequestMapping("detailedAudits")
    public void detailedAudits(@RequestParam String itemName,@RequestParam String status, @RequestParam List<String> remarkContext,HttpServletRequest httpServletRequest ,HttpServletResponse httpServletResponse) throws Exception {
        List<String> cleaningRemarkContext = new ArrayList<>();
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        StatusType statusType = CalculationIsType.calculationIsStatusType(status);
        System.out.println(remarkContext);
        for (String s : remarkContext) {
            if (s.equalsIgnoreCase("thisNull")){
                cleaningRemarkContext.add("");
            }else {
                cleaningRemarkContext.add(s);
            }
        }
        boolean b = auditService.S_C_AuditReviewNG(itemName, statusType, cleaningRemarkContext);
        if (b) {
            resultInfo.setFlag(true);

        }else{
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("审核失败");
        }
        packagedData.writeValue(resultInfo,httpServletResponse);

    }

    @RequestMapping("detailedAuditsPass")
    public void detailedAuditsPass(@RequestParam String ItemName,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException, EncodeException {
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        WebSocketServer webSocketServer = new WebSocketServer();
        MongoDB_S_C_WebSocketSendMessageEntity mongoDB_s_c_webSocketSendMessageEntity = new MongoDB_S_C_WebSocketSendMessageEntity();
        UserEntity userEntity = getUser(ItemName);
        boolean b = auditService.S_C_AuditPass(ItemName, userEntity.getUname());
        if (b) {
            resultInfo.setFlag(true);
            resultInfo.setErrorMsg("审核成功，数据已持久存储！");
            String str = "您的"+ItemName+"信息审核已通过,数据已持久存储";
            mongoDB_s_c_webSocketSendMessageEntity.setMessage(str);
            mongoDB_s_c_webSocketSendMessageEntity.setImportantNewsMessageEntity(false);
            mongoDB_s_c_webSocketSendMessageEntity.setMessageType(MessageType.NormalMessages);
            GetServerDate.getMessageTime(mongoDB_s_c_webSocketSendMessageEntity);
            webSocketServer.SendPrivateMessagesToUser(mongoDB_s_c_webSocketSendMessageEntity, userEntity.getUname());
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("审核失败！数据已删除！");
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }
    //日常点检的审核
    @RequestMapping("sCDailyPass")
    public void sCDailyPass(@RequestParam String SCSailings, @RequestParam Long date,@RequestParam String pointName,HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws IOException {
        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        Date date1 = new Date();
        date1.setTime(date);

        UserEntity userEntity = (UserEntity) httpServletRequest.getSession().getAttribute("USER");

        Permission_TBEntity permission_tbEntity = new Permission_TBEntity(10, "s4", "点检业务审核");
        List<MongoDB_S_C_SaveSCDailyContextEntity> mongoDB_s_c_webSocketMessageEntities = messageCenterService.findS_C_DailyAuditMessage(permission_tbEntity,date1,pointName);
        List<S_C_DatesEntity> scDatesEntityS = mongoDB_s_c_webSocketMessageEntities.get(0).getSCDatesEntityS();
        MongoDB_S_C_SaveSCDailyContextEntity mongoDB_s_c_saveSCDailyContextEntity = mongoDB_s_c_webSocketMessageEntities.get(0);

        int i = 0;
        System.out.println(scDatesEntityS);
        for (S_C_DatesEntity scDatesEntity : scDatesEntityS) {

            if (SCSailings.equalsIgnoreCase("早班")){
                scDatesEntity.setMorningAuditUid(userEntity.getUid());
                if (scDatesEntity.getPointContentMorningStatusId() != 1) {
                    i++;
                }
            }else {
                scDatesEntity.setMiddayAuditUid(userEntity.getUid());
                if (scDatesEntity.getPointContentMiddayStatusId() != 1) {
                    i++;
                }
            }

        }

        if (i == 0){
            mongoDB_s_c_saveSCDailyContextEntity.setSCDatesEntityS(scDatesEntityS);
            if (scDatesEntityS.get(0).getMiddayAuditUid() != null && scDatesEntityS.get(0).getMorningAuditUid() != null) {
                //删mongo,存mysql
                boolean MSQb = false;

                boolean b = spot_check_fusionService.saveS_C_DailyAudit(mongoDB_s_c_saveSCDailyContextEntity);
                if (b){
                    MSQb = mongoDB_s_c_saveDailyContextDao.deleteS_C_DailyAuditMessage(permission_tbEntity,date1,pointName);
                }
                if (MSQb){
                    resultInfo.setFlag(true);
                    resultInfo.setErrorMsg("数据已进入持久存储");
                }else {
                    resultInfo.setFlag(false);
                    resultInfo.setErrorMsg("审核失败");
                }

            }else{
                //存Mongo
                boolean b = mongoDB_s_c_saveDailyContextDao.updateSCDailyContext(mongoDB_s_c_saveSCDailyContextEntity);
                if (b){
                    resultInfo.setFlag(true);
                    resultInfo.setErrorMsg("当前班次审核成功");
                }else {
                    resultInfo.setFlag(false);
                    resultInfo.setErrorMsg("审核失败");
                }
            }
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg(pointName+"有异常!");
        }

        packagedData.writeValue(resultInfo,httpServletResponse);



    }

}
